package de.kalinasch.pa.placereminder;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import de.kalinasch.pa.placereminder.database.IncidentDataSource;
import de.kalinasch.pa.placereminder.dmo.IncidentListItemDMO;
import de.kalinasch.pa.placereminder.service.GPSService;

/**
 * This activity is the central part of the app. The activty displays all saved reminder in a list. The activity enables
 * the user to start or stop the service, that check the current position with the saved positions.
 * 
 * @author dkalinasch
 */
public class ListReminderActivity extends ListActivity implements OnClickListener {

	public void onCreate(Bundle _savedInstanceState) {
		super.onCreate(_savedInstanceState);
		setContentView(R.layout.main);
		
		Button createButton = (Button) findViewById(R.id.button_create_rem);
		createButton.setOnClickListener(this);

		Button startButton = (Button) findViewById(R.id.button_start_service);
		startButton.setOnClickListener(this);

		Button stopButton = (Button) findViewById(R.id.button_stop_service);
		stopButton.setOnClickListener(this);

		// load reminder from db
		IncidentDataSource adapter = new IncidentDataSource(this);
		ArrayList<IncidentListItemDMO> incidentArrList = adapter.listAllIncidents();

		// Service can only be started, if at least on incident is saved
		if (incidentArrList == null || incidentArrList.size() <= 0) {
			((TextView) findViewById(R.id.create_message)).setVisibility(View.VISIBLE);
			startButton.setClickable(false);
		}

		// Display reminder in list
		IncidentAdapter myAdapter = new IncidentAdapter(this, R.layout.listreminder, incidentArrList);
		setListAdapter(myAdapter);
	}

	/**
	 * Method responds to the click of a button within the activity.
	 * 
	 * @param _view
	 */
	@Override
	public void onClick(View _view) {

		if (_view.getId() == R.id.button_create_rem) {
			Intent intent = new Intent(_view.getContext(), CreateReminderActivity.class);
			_view.getContext().startActivity(intent);
			return;
		}

		Intent service = new Intent(_view.getContext(), GPSService.class);
		Button startService = (Button) findViewById(R.id.button_start_service);
		Button stopService = (Button) findViewById(R.id.button_stop_service);
		if (_view.getId() == R.id.button_start_service) {
			startService(service);
			startService.setVisibility(View.GONE);
			stopService.setVisibility(View.VISIBLE);
		} else if (_view.getId() == R.id.button_stop_service) {
			stopService(service);
			stopService.setVisibility(View.GONE);
			startService.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * This adapter creates a list entry of each incident. Title and descirption will be displayes, the id is used in
	 * the background to show the details of an incident in a other activity.
	 * 
	 * 
	 * @author Domenic
	 * 
	 */
	private class IncidentAdapter extends ArrayAdapter<IncidentListItemDMO> implements OnClickListener {

		private ArrayList<IncidentListItemDMO> incidentlist;

		public IncidentAdapter(Context _context, int _textViewResourceId, ArrayList<IncidentListItemDMO> _incidents) {
			super(_context, _textViewResourceId, _incidents);
			incidentlist = _incidents;
		}

		public View getView(int _position, View _convert, ViewGroup _parent) {
			View v = _convert;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.listreminder, null);
			}
			IncidentListItemDMO incident = incidentlist.get(_position);
			if (incident != null) {
				TextView tt = (TextView) v.findViewById(R.id.incidentlist_title);
				TextView bt = (TextView) v.findViewById(R.id.incidentlist_description);
				v.setTag(incident.getId());
				if (tt != null) {
					tt.setText(incident.getTitle());
				}
				if (bt != null) {
					bt.setText(incident.getDescription());
				}
			}
			v.setOnClickListener(this);
			return v;
		}

		/**
		 * Listener for each list entry. If a entry of the list is clicked by the user, the full data of the enty will
		 * be displayed in another activity.
		 * 
		 * @param _item
		 */
		public void onClick(View _item) {
			int id = (Integer) _item.getTag();
			Intent intent = new Intent(_item.getContext(), ShowReminderDetailActivity.class);
			intent.putExtra(ShowReminderDetailActivity.INCIDENT_ID, id);
			startActivity(intent);
		}
	}
}
